<template>
  <div class="app-container">
    <el-form :model="queryParams"
      ref="queryForm"
      size="small"
      :inline="true"
      v-show="showSearch"
      class="form-search"
      label-width="120px">
      <!-- <el-form-item :label="$t('mgu.exchangeName')"
        prop="exchangeName">
        <el-input v-model="queryParams.exchangeName"
          :placeholder="$t('common.placeholderContent')"
          clearable
          style="width: 240px"
          maxlength="100"
          @keyup.enter.native="handleQuery" />
      </el-form-item> -->
      <el-form-item :label="$t('mgu.payoutExchangeName')"
        prop="exchangeId">
        <el-select v-model="queryParams.exchangeId"
          filterable
          remote
          reserve-keyword
          :placeholder="$t('common.placeholderSelect')"
          :remote-method="remoteMethod3"
          :loading="exhloading3"
          style="width: 240px">
          <el-option v-for="item in exchangeList3"
            :key="item.exchangeId"
            :label="item.exchangeName"
            :value="item.exchangeId">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item :label="$t('mgu.userName')"
        prop="userName">
        <el-input v-model="queryParams.userName"
          :placeholder="$t('common.placeholderContent')"
          clearable
          style="width: 240px"
          maxlength="50"
          @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item :label="$t('mgu.accountName')"
        prop="accountName">
        <el-input v-model="queryParams.accountName"
          :placeholder="$t('common.placeholderContent')"
          clearable
          style="width: 240px"
          maxlength="50"
          @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item :label="$t('mtd.transDate')">
        <el-date-picker v-model="dateRange"
          style="width: 240px"
          value-format="yyyy-MM-dd"
          type="daterange"
          range-separator="-"
          :start-placeholder="$t('common.startDate')"
          :end-placeholder="$t('common.endDate')"></el-date-picker>
      </el-form-item>

      <el-form-item>
        <el-button type="primary"
          icon="el-icon-search"
          size="mini"
          @click="handleQuery">{{$t('common.search')}}</el-button>
        <el-button icon="el-icon-refresh"
          size="mini"
          @click="resetQuery">{{$t('common.reset')}}</el-button>
      </el-form-item>
    </el-form>

    <div class="section-box">
      <el-row :gutter="10"
        class="mb8">
        <el-col :span="1.5">
          <el-button type="primary"
            plain
            icon="el-icon-plus"
            size="mini"
            @click="handleAdd">{{$t('common.add')}}</el-button>
        </el-col>
        <!-- <el-col :span="1.5">
          <el-button type="success"
            plain
            icon="el-icon-edit"
            size="mini"
            :disabled="single"
            @click="handleUpdate">{{$t('common.edit')}}</el-button>
        </el-col>
        <el-col :span="1.5">
          <el-button type="danger"
            plain
            icon="el-icon-delete"
            size="mini"
            :disabled="multiple"
            @click="handleDelete">{{$t('common.delete')}}</el-button>
        </el-col> -->
        <right-toolbar :showSearch.sync="showSearch"
          @queryTable="getList"></right-toolbar>
      </el-row>

      <el-table v-loading="loading"
        :data="recordList">
        <!-- <el-table-column type="selection"
          width="55"
          align="center" /> -->
        <el-table-column :label="$t('mgu.payoutExchangeName')"
          align="center"
          prop="exchangeName"
          min-width="160"
          show-overflow-tooltip />
        <el-table-column :label="$t('mgu.amount')"
          align="center"
          prop="amount"
          min-width="120" />
        <el-table-column :label="$t('mgu.userName')"
          align="center"
          prop="userName"
          :show-overflow-tooltip="true"
          min-width="150" />
        <el-table-column :label="$t('mgu.accountName')"
          align="center"
          prop="accountName"
          :show-overflow-tooltip="true"
          min-width="150" />
        <el-table-column :label="$t('mgu.status')"
          align="center"
          min-width="100">
          <template slot-scope="scope">
            <!-- <el-switch v-model="scope.row.status"
              active-value="1"
              inactive-value="0"
              @change="handleStatusChange(scope.row)"></el-switch> -->
            <dict-tag :options="dict.type.sys_data_status"
              :value="scope.row.status" />
          </template>
        </el-table-column>
        <el-table-column :label="$t('common.createBy')"
          align="center"
          prop="createBy"
          min-width="160"
          show-overflow-tooltip />
        <el-table-column :label="$t('common.createTime')"
          align="center"
          prop="createTime"
          min-width="180">
          <template slot-scope="scope">
            <span>{{ parseTime(scope.row.createTime) }}</span>
          </template>
        </el-table-column>
        <el-table-column :label="$t('common.updateBy')"
          align="center"
          prop="updateBy"
          min-width="160"
          show-overflow-tooltip />
        <el-table-column :label="$t('common.updateTime')"
          align="center"
          prop="updateTime"
          min-width="180">
          <template slot-scope="scope">
            <span>{{ parseTime(scope.row.updateTime) }}</span>
          </template>
        </el-table-column>
        <el-table-column :label="$t('common.remark')"
          prop="remark"
          min-width="160"
          show-overflow-tooltip />
        <!-- <el-table-column :label="$t('common.controls')"
          align="center"
          class-name="small-padding fixed-width"
          fixed="right"
          min-width="140">
          <template slot-scope="scope">
            <el-button size="mini"
              type="text"
              icon="el-icon-edit"
              @click="handleUpdate(scope.row)">{{$t('common.edit')}}</el-button>
            <el-button size="mini"
              type="text"
              icon="el-icon-delete"
              @click="handleDelete(scope.row)">{{$t('common.delete')}}</el-button>
          </template>
        </el-table-column> -->
      </el-table>

      <pagination v-show="total > 0"
        :total="total"
        :page.sync="queryParams.pageNum"
        :limit.sync="queryParams.pageSize"
        @pagination="getList" />
    </div>

    <!-- 添加或修改角色配置对话框 -->
    <el-dialog :title="title"
      :visible.sync="open"
      width="600px"
      append-to-body>
      <el-form ref="form"
        :model="form"
        :rules="rules"
        label-width="140px">
        <el-form-item :label="$t('mgu.payoutExchangeName')"
          prop="exchangeId">
          <el-select v-model="form.exchangeId"
            filterable
            remote
            reserve-keyword
            :placeholder="$t('common.placeholderSelect')"
            :remote-method="remoteMethod"
            :loading="exhloading"
            style="width: 100%">
            <el-option v-for="item in exchangeList"
              :key="item.exchangeId"
              :label="item.exchangeName"
              :value="item.exchangeId">
            </el-option>
          </el-select>
        </el-form-item>

        <el-form-item :label="$t('mgu.amount')"
          prop="amount">
          <el-input v-model="form.amount"
            maxlength="20"
            @input="form.amount = onInput(form.amount)"
            :placeholder="$t('common.placeholderContent')">
            <template slot="append">{{$t('common.U')}}</template>
          </el-input>
        </el-form-item>
        <el-form-item :label="$t('mgu.userName')"
          prop="userName">
          <el-input v-model="form.userName"
            maxlength="50"
            :placeholder="$t('common.placeholderContent')" />
        </el-form-item>
        <el-form-item :label="$t('mgu.accountName')"
          prop="accountName">
          <el-input v-model="form.accountName"
            maxlength="50"
            :placeholder="$t('common.placeholderContent')" />
        </el-form-item>
        <!-- <el-form-item :label="$t('mgu.status')">
          <el-radio-group v-model="form.status">
            <el-radio v-for="dict in dict.type.sys_data_status"
              :key="dict.value"
              :label="dict.value">{{ dict.label }}</el-radio>
          </el-radio-group>
        </el-form-item> -->
        <el-form-item :label="$t('common.remark')">
          <el-input v-model="form.remark"
            type="textarea"
            maxlength="200"
            :placeholder="$t('common.placeholderContent')"></el-input>
        </el-form-item>
        <el-form-item :label="$t('mgu.attachment')">
          <ImageUpload :limit="9"
            v-model="form.attachment" />
        </el-form-item>
      </el-form>
      <div slot="footer"
        class="dialog-footer">
        <el-button type="primary"
          @click="submitForm">{{$t('common.ok')}}</el-button>
        <el-button @click="cancel">{{$t('common.cancel')}}</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  getUList,
  URecordDetail,
  addURecord,
  updateURecord,
  delURecord,
  changeURecordStatus,
} from "@/api/transaction/merchantGetU.js";
import {
  getExhUserList,
} from "@/api/system/exchangeUser.js";
import { onInput } from '@/utils/util.js'

export default {
  name: "merchantGetU",
  dicts: ["sys_data_status"],
  data () {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 角色表格数据
      recordList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 日期范围
      dateRange: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        userName: null,
        exchangeName: null,
        exchangeId: null,
        accountName: null,
        startTime: null,
        startTime: null,
        recordType: 'payout'
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        exchangeId: [
          { required: true, message: this.$t('common.ruleSelectEmpty'), trigger: "change" },
        ],
        amount: [
          { required: true, message: this.$t('common.ruleInputEmpty'), trigger: "blur" },
        ],
        userName: [
          { required: true, message: this.$t('common.ruleInputEmpty'), trigger: "blur" },
        ],
        accountName: [
          { required: true, message: this.$t('common.ruleInputEmpty'), trigger: "blur" },
        ],
      },

      exchangeList: [],
      exhloading: false,
      exhloading3: false,
      exchangeList3: [],
    };
  },
  watch: {
    dateRange (val) {
      if (val) {
        this.queryParams.startTime = `${val[0]} 00:00:00`
        this.queryParams.endTime = `${val[1]} 23:59:59`
      } else {
        this.queryParams.startTime = null
        this.queryParams.endTime = null
      }
    },
  },
  created () {
    this.getList();
  },
  methods: {
    onInput,
    // 搜索汇兑用户
    remoteMethod (query) {
      if (query !== '') {
        this.exhloading = true;
        getExhUserList({
          pageNum: 1,
          pageSize: 10,
          payType: 'payout',
          status: 1,
          exchangeName: query
        }).then(
          (response) => {
            this.exchangeList = response.rows;
            this.exhloading = false;
          }
        );
      } else {
        this.exchangeList = [];
      }
    },
    remoteMethod3 (query) {
      if (query !== '') {
        this.exhloading3 = true;
        getExhUserList({
          pageNum: 1,
          pageSize: 10,
          payType: 'payout',
          status: 1,
          exchangeName: query
        }).then(
          (response) => {
            this.exchangeList3 = response.rows;
            this.exhloading3 = false;
          }
        );
      } else {
        this.exchangeList3 = [];
      }
    },
    /** 查询列表 */
    getList () {
      this.loading = true;
      getUList(this.queryParams).then(
        (response) => {
          this.recordList = response.rows;
          this.total = response.total;
          this.loading = false;
        }
      );
    },
    // 状态修改
    handleStatusChange (row) {
      // let text = row.status === "0" ? "启用" : "停用";
      let text = '';
      let tips = ''
      if (row.status === "1") {
        text = `${this.$t('common.statusChangesEnable')} ${row.userName} ?`
        tips = this.$t('common.enabledSuccessfully')
      } else {
        text = `${this.$t('common.statusChangesDisable')} ${row.userName} ?`
        tips = this.$t('common.disableSuccessfully')
      }
      this.$modal
        // .confirm('确认要"' + text + '""' + row.roleName + '"吗？')
        .confirm(text)
        .then(function () {
          return changeURecordStatus({
            status: row.status,
            recordId: row.recordId
          });
        })
        .then(() => {
          this.$modal.msgSuccess(tips);
        })
        .catch(function () {
          row.status = row.status === "0" ? "1" : "0";
        });
    },
    // 取消按钮
    cancel () {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset () {
      this.form = {
        recordType: 'payout',
        exchangeId: null,
        amount: null,
        userName: null,
        accountName: null,
        status: '1',
        remark: '',
        attachment: ''
      };
      this.exchangeList = [];
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery () {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery () {
      this.resetForm("queryForm");
      this.exchangeList3 = [];
      this.dateRange = []
      this.queryParams.startTime = null
      this.queryParams.endTime = null
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange (selection) {
      this.ids = selection.map((item) => item.recordId);
      this.single = selection.length != 1;
      this.multiple = !selection.length;
    },

    /** 新增按钮操作 */
    handleAdd () {
      this.reset();
      this.open = true;
      this.title = this.$t('mgu.addRecord');
    },
    /** 修改按钮操作 */
    handleUpdate (row) {
      this.reset();
      const recordId = row.recordId || this.ids;
      URecordDetail(recordId).then((response) => {
        this.form = response.data;
        this.exchangeList = [{
          exchangeId: this.form.exchangeId,
          exchangeName: this.form.exchangeName
        }];
        this.open = true;
        this.title = this.$t('mgu.eidtRecord');
      });
    },
    /** 提交按钮 */
    submitForm: function () {
      this.$refs["form"].validate((valid) => {
        if (valid) {
          if (this.form.recordId != undefined) {
            updateURecord(this.form).then((response) => {
              this.$modal.msgSuccess(this.$t('common.editSuccessfully'));
              this.open = false;
              this.getList();
            });
          } else {
            this.$modal
              .confirm(this.$t('common.addNotOptTips'))
              .then(() => {
                addURecord(this.form).then((response) => {
                  this.$modal.msgSuccess(this.$t('common.newSuccess'));
                  this.open = false;
                  this.getList();
                });
              })
              .catch(() => { });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete (row) {
      const recordIds = row.recordId || this.ids;
      this.$modal
        .confirm(this.$t('common.deleteDataTips'))
        .then(function () {
          return delURecord(recordIds);
        })
        .then(() => {
          this.getList();
          this.$modal.msgSuccess(this.$t('common.successfullyDeleted'));
        })
        .catch(() => { });
    }
  },
};
</script>
